package store.swing.order;

import store.domain.Order;
import store.domain.OrderStatus;
import store.domain.PaymentMethod;
import store.domain.ShippingMethod;
import store.swing.DefaultFrame;
import store.util.AppUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class OrderInfoFrame extends DefaultFrame {
    private static final List<ShippingMethod> SHIPPING_METHODS = AppUtils.getService("shippingMethodService").readAll();
    private static final List<PaymentMethod> PAYMENT_METHODS = AppUtils.getService("paymentMethodService").readAll();

    private Order order;
    private JComboBox orderStatus;

    public OrderInfoFrame(Order order) {
        this.order = order;
        setTitle("Информация о заказе");
        initComponents();
    }

    private void initComponents() {
        JPanel infoPanel = new JPanel(new GridBagLayout());

        GridBagLayout layout = (GridBagLayout) infoPanel.getLayout();
        layout.columnWidths = new int[]{0, 0, 0, 0};
        layout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        layout.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0E-4};
        layout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 10, 5, 5);
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.BOTH;

        JLabel orderNumberLabel = new JLabel("Номер заказа:");
        c.gridx = 0;
        c.gridy = 0;
        infoPanel.add(orderNumberLabel, c);

        JLabel orderNumberValueLabel = new JLabel(order.getOrderNumber());
        c.gridx = 1;
        c.gridy = 0;
        infoPanel.add(orderNumberValueLabel, c);

        JLabel orderDateLabel = new JLabel("Дата заказа:");
        c.gridx = 0;
        c.gridy = 1;
        infoPanel.add(orderDateLabel, c);

        JLabel orderDateValueLabel = new JLabel(order.getOrderDate().toString());
        c.gridx = 1;
        c.gridy = 1;
        infoPanel.add(orderDateValueLabel, c);

        JLabel orderStatusLabel = new JLabel("Статус заказа:");
        c.gridx = 0;
        c.gridy = 2;
        infoPanel.add(orderStatusLabel, c);

        orderStatus = new JComboBox<>(getStatusNames().toArray());
        orderStatus.setSelectedIndex(order.getOrderStatus() - 1);
        c.gridx = 1;
        c.gridy = 2;
        infoPanel.add(orderStatus, c);

        JButton orderStatusButton = new JButton("Изменить статус");
        orderStatusButton.addActionListener(new orderStatusButtonListener());
        c.gridx = 1;
        c.gridy = 3;
        infoPanel.add(orderStatusButton, c);

        JLabel fullNameLabel = new JLabel("Полное имя:");
        c.gridx = 0;
        c.gridy = 4;
        infoPanel.add(fullNameLabel, c);

        JLabel fullNameValueLabel = new JLabel(order.getfName() + " " + order.getlName());
        c.gridx = 1;
        c.gridy = 4;
        infoPanel.add(fullNameValueLabel, c);

        JLabel phoneLabel = new JLabel("Телефон:");
        c.gridx = 0;
        c.gridy = 5;
        infoPanel.add(phoneLabel, c);

        JLabel phoneValueLabel = new JLabel(order.getPhone());
        c.gridx = 1;
        c.gridy = 5;
        infoPanel.add(phoneValueLabel, c);

        JLabel emailLabel = new JLabel("E-mail:");
        c.gridx = 0;
        c.gridy = 6;
        infoPanel.add(emailLabel, c);

        JTextField emailValueLabel = new JTextField(order.getEmail());
        emailValueLabel.setEditable(false);
        c.gridx = 1;
        c.gridy = 6;
        infoPanel.add(emailValueLabel, c);

        JLabel cityLabel = new JLabel("Город:");
        c.gridx = 0;
        c.gridy = 7;
        infoPanel.add(cityLabel, c);

        JLabel cityValueLabel = new JLabel(order.getCity());
        c.gridx = 1;
        c.gridy = 7;
        infoPanel.add(cityValueLabel, c);

        JLabel streetLabel = new JLabel("Улица:");
        c.gridx = 0;
        c.gridy = 8;
        infoPanel.add(streetLabel, c);

        JLabel streetValueLabel = new JLabel(order.getStreet());
        c.gridx = 1;
        c.gridy = 8;
        infoPanel.add(streetValueLabel, c);

        JLabel apartmentLabel = new JLabel("Квартира");
        c.gridx = 0;
        c.gridy = 9;
        infoPanel.add(apartmentLabel, c);

        JLabel apartmentValueLabel = new JLabel(order.getApartment());
        c.gridx = 1;
        c.gridy = 9;
        infoPanel.add(apartmentValueLabel, c);

        JLabel shippingMethodLabel = new JLabel("Доставка:");
        c.gridx = 0;
        c.gridy = 10;
        infoPanel.add(shippingMethodLabel, c);

        JLabel shippingMethodValueLabel = new JLabel(getShippingMethodName());
        c.gridx = 1;
        c.gridy = 10;
        infoPanel.add(shippingMethodValueLabel, c);

        JLabel paymentMethodLabel = new JLabel("Оплата:");
        c.gridx = 0;
        c.gridy = 11;
        infoPanel.add(paymentMethodLabel, c);

        JLabel paymentMethodValueLabel = new JLabel(getPaymentMethodName());
        c.gridx = 1;
        c.gridy = 11;
        infoPanel.add(paymentMethodValueLabel, c);

        JLabel totalLabel = new JLabel("Сумма:");
        c.gridx = 0;
        c.gridy = 12;
        infoPanel.add(totalLabel, c);

        int total = order.getOrderTotal().intValue();
        JLabel totalValueLabel = new JLabel(String.valueOf(total) + " " + order.getCurrencyCode());
        c.gridx = 1;
        c.gridy = 12;
        infoPanel.add(totalValueLabel, c);

        OrderItemTable orderItemTable = new OrderItemTable();
        JScrollPane scrollPane = new JScrollPane(orderItemTable);
        c.gridx = 2;
        c.gridy = 0;
        c.gridheight = 14;
        infoPanel.add(scrollPane, c);

        add(infoPanel);
    }

    private List getStatusNames() {
        List<OrderStatus> statuses = OrderTableModel.getOrderStatuses();
        List<String> statusNames = new ArrayList<>();
        for (OrderStatus status : statuses) {
            statusNames.add(status.getNameRuRu());
        }
        return statusNames;
    }

    private String getShippingMethodName() {
        Integer shippingMethodId = order.getShippingMethodId();
        String shippingMethodName = "";
        for (ShippingMethod shippingMethod : SHIPPING_METHODS) {
            if (shippingMethod.getShippingId().equals(shippingMethodId)) {
                shippingMethodName = shippingMethod.getNameRuRu();
                return shippingMethodName;
            }
        }
        return shippingMethodName;
    }

    private String getPaymentMethodName() {
        Integer paymentMethodId = order.getPaymentMethodId();
        String paymentMethodName = "";
        for (PaymentMethod paymentMethod : PAYMENT_METHODS) {
            if (paymentMethod.getPaymentId().equals(paymentMethodId)) {
                paymentMethodName = paymentMethod.getNameRuRu();
                return paymentMethodName;
            }
        }
        return paymentMethodName;
    }

    private class orderStatusButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = orderStatus.getSelectedIndex();
            order.setOrderStatus((byte) (selectedIndex + 1));
            AppUtils.getService("orderService").update(order);
            OrderTable.TABLE_MODEL.fireTableDataChanged();
        }
    }
}
